/*
 * BoardSquare.fx
 *
 * Created on 2009-09-04, 11:42:25
 */

package shmallarchitect.gui;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.paint.Color;

import javafx.animation.Timeline;


import javafx.scene.shape.Rectangle;

import javafx.scene.Group;

import javafx.scene.input.MouseEvent;


/**
 * @author Administrator
 */

public class BoardSquare extends CustomNode {
    public var pieceWidth : Number;
    public var pieceHeight : Number;
    public-init var positionX : Integer;
    public-init var positionY : Integer;
    //public var content : Piece;
    public-init var board : Board;

    public var squareID: Integer;
    public var selected = false;
    public var localid: Integer = 0;
    public var bgcolor = Color.web("#6b6b6b");
    var a: Boolean = bind BaseLayout.editmode;

    def square = Rectangle    {
        x: positionX * pieceWidth;
        y: positionY * pieceHeight;
        width: pieceWidth-1;
        height: pieceHeight-1;
        fill: bind bgcolor;
        stroke: Color.web("#535353");
        strokeWidth: 0;
    }



def hoverstate = Timeline {
   keyFrames: [
   at(0s) {
           square.stroke => Color.web("#535353");
           },

   at(0.05s) {
           square.stroke => Color.web("#79b8f4")
    
           }
         ]
}


    // Inne

    override function create():Node {
       return Group {
                 content: [ square ]
                            onMouseEntered: function(event) {

                                 square.strokeWidth = 1;
                                 hoverstate.rate = 1;
                                 hoverstate.play();

                                 BaseLayout.hoverid = squareID;


                                 },

                            onMouseExited: function(event) {
                                 if (selected == false) {
                                 hoverstate.rate = -1;
                                 hoverstate.play();
                                 square.strokeWidth = 0;
                                 }
                                 }

                            onMousePressed: function(event: MouseEvent){
                                    
                                if(a) { // EditMode

                                if(event.primaryButtonDown) {
                                
                                if (selected)
                                {       square.strokeWidth = 0;
                                        square.stroke = Color.web("#535353");
                                delete squareID from BaseLayout.sequence;

                                }
                                else { square.strokeWidth = 1;
                                       square.stroke = Color.WHITE;
                                       insert squareID into BaseLayout.sequence;
                                }
                                selected = not selected;
                                }

                                // Prawy guzik myszy
                                //else if (event.secondaryButtonDown)
                                        //{ BaseLayout.debug = "RMB" }
                                
                                }
                                //ViewMode
                                else {

                                   DisplayLocum.displayed = localid;
                                   DisplayLocum.displayedLocum = BaseLayout.local_list[localid];
            
                                   BaseLayout.mode = false;
                                   DisplayLocum.view_mode = true;


                                   println(localid);
                                   println(BaseLayout.local_list[localid].resource_list);
                                   println(DisplayLocum.displayedLocum.resource_list);
                                    
                                }


                                }

                };
    }

    public function getXPixel() : Number
    {
        return positionX * pieceWidth;
    }

    public function getYPixel() : Number
    {
        return positionY * pieceHeight;
    }
}